﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CCWin;

namespace weiqijiance
{
    public partial class renyuanshezhi : CCSkinMain
    {
        public mainform m_mainform;

        public renyuanshezhi()
        {
            InitializeComponent();
            
        }

        private void btncsqyadd_Click(object sender, EventArgs e)
        {
            if (txtcsqycode1.Text != txtcsqycode2.Text)
            {
                MessageBox.Show("密码不一致!", "提示", MessageBoxButtons.OK);
                return;
            }

            string ssqlex = "";
            ssqlex = string.Format(@"select * from Employee where EP_ID='{0}'", txtcsqyid.Text);
            DataTable dt = m_mainform.objbassLW.objdb.GetTable(ssqlex);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("已存在该编号人员");
                return;
            }
            else
            {

                ssqlex = string.Format(@"insert into Employee (EP_ID,EP_NAME,EP_TYPE,EP_RIGHT,EP_PASS) values ('{0}','{1}','{2}','{3}','{4}')", txtcsqyid.Text, txtcsqyname.Text, comboBox1.Text, "111111", txtcsqycode1.Text);
                if (!m_mainform.objbassLW.objdb.InsertDB(ssqlex))
                {
                    MessageBox.Show("保存失败");
                }
            }
            adduser();
          

        }

        private void btncsqyupdate_Click(object sender, EventArgs e)
        {
            
            if (txtcsqycode1.Text != txtcsqycode2.Text)
            {
                MessageBox.Show("密码不一致!", "提示", MessageBoxButtons.OK);
                return;
            }
            string ssqlex = "";
            ssqlex = string.Format(@"update Employee set  EP_NAME='{0}',EP_PASS='{1}' where EP_ID='{2}'", txtcsqyname.Text, txtcsqycode1.Text, txtcsqyid.Text);
            if (!m_mainform.objbassLW.objdb.InsertDB(ssqlex))
            {
                MessageBox.Show("更新失败");
            }
            adduser();
        }

        private void btncsqydel_Click(object sender, EventArgs e)
        {
             

            string ssqlex = "";
            ssqlex = string.Format(@"delete Employee where EP_ID='{0}'", txtcsqyid.Text);
            if (!m_mainform.objbassLW.objdb.InsertDB(ssqlex))
            {
                MessageBox.Show("删除失败");
            }
            adduser();
           
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            groupBox1.Text = comboBox1.Text;
            string str = comboBox1.Text;
            switch (str)
            {
                case "引车员": inicbx(lvcsqy, "YCY"); break;
                case "操作员": inicbx(lvcsqy, "CZY"); break;
                
            }
        }

        void inicbx(ListView cbx, string dataname)
        {
            cbx.Items.Clear();
     
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0}", dataname);
            DataTable dt = m_mainform.objbassLW.objdb.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
              
                ListViewItem li = new ListViewItem();
                li.SubItems[0].Text = dt.Rows[i]["id"].ToString().Trim();
                li.SubItems.Add(dt.Rows[i]["xh"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["name"].ToString().Trim());
                cbx.Items.Add(li);
            }
        }
        public void add(string stable, string sxh, string sname)
        {
           
            string ssqlex = "";
            ssqlex = string.Format(@"insert into {0} (xh,name) values ('{1}','{2}')", stable, sxh, sname);
            if (!m_mainform.objbassLW.objdb.InsertDB(ssqlex))
            {
                MessageBox.Show("添加失败");
            }
        }
        public void update(string stable, string sid, string sxh, string sname)
        {
           
            string ssqlex = "";
            ssqlex = string.Format(@"update {0} set xh='{1}',name='{2}' where id='{3}'", stable, sxh, sname, sid);
            if (!m_mainform.objbassLW.objdb.InsertDB(ssqlex))
            {
                MessageBox.Show("修改失败");
            }
        }
        public void del(string stable, string sid)
        {
           
            string ssqlex = "";
            ssqlex = string.Format(@"delete {0} where id='{1}'", stable, sid);
            if (!m_mainform.objbassLW.objdb.InsertDB(ssqlex))
            {
                MessageBox.Show("删除失败");
            }
        }

        private void lvcsqy_SelectedIndexChanged(object sender, EventArgs e)
        {
            int k = lvcsqy.SelectedItems.Count;

            foreach (ListViewItem item in lvcsqy.SelectedItems)
            {
                txtcsqyid.Text = item.SubItems[0].Text.ToString().Trim();
                comboBox1.Text = item.SubItems[2].Text.ToString().Trim();
                txtcsqyname.Text = item.SubItems[1].Text.ToString().Trim();
              
            }
            string ssqlex = "";
            ssqlex = string.Format(@"Select * From Employee where EP_ID='{0}'", txtcsqyid.Text);
            DataTable dt = m_mainform.objbassLW.objdb.GetTable(ssqlex);
            for (int i = 0; i < 1; i++)
            {
                txtcsqyid.Text = dt.Rows[i]["EP_ID"].ToString().Trim();
                txtcsqyname.Text = dt.Rows[i]["EP_NAME"].ToString().Trim();
                txtcsqycode1.Text = dt.Rows[i]["EP_PASS"].ToString().Trim();
                txtcsqycode2.Text = dt.Rows[i]["EP_PASS"].ToString().Trim();
                comboBox1.Text = dt.Rows[i]["EP_TYPE"].ToString().Trim();
                
            }

        }
        public void adduser()
        {
            try
            {
                txtcsqyid.Text = "";
                txtcsqyname.Text = "";
                txtcsqycode1.Text = "";
                txtcsqycode2.Text = "";
                comboBox1.Text = "";
                lvcsqy.Items.Clear();
                string sql = string.Format(@"select * from Employee order by EP_ID asc");
                DataTable dt = m_mainform.objbassLW.objdb.GetTable(sql);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListViewItem li = new ListViewItem();
                        li.SubItems[0].Text = dt.Rows[i]["EP_ID"].ToString().Trim();
                        li.SubItems.Add(dt.Rows[i]["EP_NAME"].ToString().Trim());
                        li.SubItems.Add(dt.Rows[i]["EP_TYPE"].ToString().Trim());
                        lvcsqy.Items.Add(li);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void renyuanshezhi_Load(object sender, EventArgs e)
        {
            adduser();
        }

     

  
    }
}
